import Vue from 'vue'
import VueRouter from 'vue-router'
// 普通路由组件导入方式
// import Login from '@/components/Login'
// import Home from '@/components/Home'
// import Welcome from '@/components/Welcome'
// import Users from '@/views/User/Users'
// import Rights from '@/views/power/Rights'
// import Roles from '@/views/power/Roles'
// import Cate from '@/views/goods/Cate'
// import Params from '@/views/goods/Params'
// import GoodsList from '@/views/goods/List'
// import AddGoods from '@/views/goods/Add'
// import OrderList from '@/views/order/Order'
// import Report from '@/views/report/Report'

// 懒加载路由组件导入方式
const Login = () => import(/* webpackChunkName: "login_home_welcome" */ '@/components/Login')
const Home = () => import(/* webpackChunkName: "login_home_welcome" */ '@/components/Home')
const Welcome = () => import(/* webpackChunkName: "login_home_welcome" */ '@/components/Welcome')

const Users = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '@/views/User/Users')
const Rights = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '@/views/power/Rights')
const Roles = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '@/views/power/Roles')

const Cate = () => import(/* webpackChunkName: "Cate_Params" */ '@/views/goods/Cate')
const Params = () => import(/* webpackChunkName: "Cate_Params" */ '@/views/goods/Params')

const GoodsList = () => import(/* webpackChunkName: "GoodsList_AddGoods" */ '@/views/goods/List')
const AddGoods = () => import(/* webpackChunkName: "GoodsList_AddGoods" */ '@/views/goods/Add')

const OrderList = () => import(/* webpackChunkName: "OrderList_Report" */ '@/views/order/Order')
const Report = () => import(/* webpackChunkName: "OrderList_Report" */ '@/views/report/Report')

Vue.use(VueRouter)

const routes = [
  // 路由重定向
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/categories', component: Cate },
      { path: '/params', component: Params },
      { path: '/goods', component: GoodsList },
      { path: '/goods/add', component: AddGoods },
      { path: '/orders', component: OrderList },
      { path: '/reports', component: Report }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表用从哪个路径跳转而来
  // next 是一个函数，表示放行
  // next()放行 next('/login') 强制跳转
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})
export default router
